package com.haolijun.eecms.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.haolijun.eecms.common.R;
import com.haolijun.eecms.controller.form.InsertUserForm;
import com.haolijun.eecms.controller.form.LoginForm;
import com.haolijun.eecms.controller.form.SearchUserByPageForm;
import com.haolijun.eecms.controller.form.UpdateUserForm;
import com.haolijun.eecms.entity.TbUser;
import com.haolijun.eecms.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Set;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author hljstart
 * @Date 2023/3/7 22:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Tag(name = "UserController",description = "用户Web接口")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "登录系统")
    public R login(@Valid @RequestBody LoginForm form){
        HashMap map = JSONUtil.parse(form).toBean(HashMap.class);
        Integer userId = userService.login(map);
        boolean result = userId != null;
        // .put("result", result);

        if (result){
            R r = R.ok();
            // 登录成功 satoken
            StpUtil.login(userId);
            // 查询用户角色
            Set<String> roles = userService.searchUserRole(userId);
            // 获取token : ddasdasdasdasdasda
            String token = StpUtil.getTokenInfo().getTokenValue();
            // 查询用户信息
            HashMap userMap = userService.searchUserSummary(userId);
            // 返回给前端数据
            r.data(userMap).data("token", token).data("roles", roles);
            return r;
        }
        return R.error();
    }

    @PostMapping("/searchUserByPage")
    @Operation(summary = "查询用户分页记录")
    public R searchUserByPage(@Valid @RequestBody SearchUserByPageForm form){
        HashMap param = JSONUtil.parseObj(form).toBean(HashMap.class);
        R r = userService.searchUserByPage(param);
        return r;
    }

    @GetMapping("/searchById/{userId}")
    @Operation(summary = "用户信息回显")
    public R searchById(@PathVariable("userId") Integer userId){
        HashMap userMap = userService.searUserById(userId);
        R r = R.ok().data(userMap);
        return r;
    }

    // 添加用户
    @PostMapping("/insert")
    @Operation(summary = "添加用户信息")
    public R insert(@Valid @RequestBody InsertUserForm form){
        TbUser user = JSONUtil.parseObj(form).toBean(TbUser.class);
        int rows = userService.insert(user);
        return rows > 0 ? R.ok(): R.error();
    }

    @PostMapping("/update")
    @Operation(summary = "更新用户信息")
    public R update(@Valid @RequestBody UpdateUserForm form){
        TbUser user = JSONUtil.parseObj(form).toBean(TbUser.class);
        int rows = userService.update(user);
        return rows > 0 ? R.ok():R.error();
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    @Operation(summary = "退出登录")
    public R logout(){
        StpUtil.logout();
        return R.ok();
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @GetMapping("/delete/{uid}")
    @Operation(summary = "删除用户")
    public R deleteUserById(@PathVariable("uid") Integer uid){
        return userService.deleteUserById(uid);
    }

}
